<?php
namespace app\client\controller;
use app\client\wxpay\Notify as WxNotify;
use app\client\wxpay\Config;
use think\swoole\facade\Task;

class Notify {
    
    private $notify = null;
    private $config = null;
    
    public function __construct() {
        $this->notify = new WxNotify();
        $this->config = new Config();
    }
    
    //充电通知
    public function chargeNotify() {
        try {
            $xml = request()->getInput();
            if (!empty($xml)) {
                $_POST['WX_NOTIFY_XML'] = $xml;
                $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
                if(!empty($values['attach'])){ //是特约代理商
                    $attach = json_decode($values['attach'], true);
                    unset($values);
                    $agentId = ($attach['count'] - 325) / 1314;
                    
                    $agent = model('Agent')->findOne(['agent_id' => $agentId], ['mch_id', 'mch_key']);
                    if(!empty($agent)) {
                        $this->config->setMerchantId($agent['mch_id']);
                        $this->config->setKey($agent['mch_key']);
                    }
                }
                
                $this->notify->action = 'chargeNotify';
                $rs = $this->notify->Handle($this->config, false);
                $this->_log(1);
            }
            
        } catch (\Exception $e) {
            
        }
        
        return successResult();
        
    }
    
    //充电通知
    public function extendChargeNotify() {
        try {
            
            $xml = request()->getInput();
            if (!empty($xml)) {
                $_POST['WX_NOTIFY_XML'] = $xml;
                $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
                if(!empty($values['attach'])){ //
                    $attach = json_decode($values['attach'], true);
                    unset($values);
                    $agentId = ($attach['count'] - 325) / 1314;
                    
                    $agent = model('Agent')->findOne(['agent_id' => $agentId], ['mch_id', 'mch_key']);
                    if(!empty($agent)) {
                        $this->config->setMerchantId($agent['mch_id']);
                        $this->config->setKey($agent['mch_key']);
                    }
                }
                
                $this->notify->action = 'extendChargeNotify';
                $this->notify->Handle($this->config, false);
                $this->_log(2);
            }
            
            
            
        } catch (\Exception $e) {
            
        }
        
        return successResult();
    }
    

    
    private function _log($type) {
        if(!$this->notify->payResult){
            $data = $this->notify->callbackData;
            $orderNo = $this->notify->orderNo;
            $tradeNo = $this->notify->tradeNo;
            $msg = $this->notify->msg;
            
            $logModel = model('PayErrorLog');
            
            Task::async(function() use($data, $orderNo, $tradeNo, $msg, $logModel, $type){
                try {
                    $logModel->add(['order_no' => $orderNo, 'trade_no' => $tradeNo, 'msg' => $msg, 'data' => $data, 'type' => $type, 'add_time' => date('Y-m-d H:i:s')]);
                    unset($logModel);
                } catch (\Exception $e) {
                    
                }
            });
        }
    }
}



